<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding: 0;
        }

        div{
            width: 100px;
            height: 100px;
            background-color: skyblue;
            position: absolute;
            left: 0;
            top: 0;
        }

        p{
            width:100px;
            height:100px;
            background-color: orange;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>
<body>
    <div></div>
    <p></p>
    <script>

        var divele = document.querySelector('div');

        // divele.onclick = function(){
        //     // divele.style.left = '500px';
        //     var init = 0;
        //     const timer = setInterval(()=>{
        //         init += 5;

        //         divele.style.left = init + 'px';
        //         if(init>=500){
        //             clearInterval(timer);
        //         }
        //     },50)
        // }

        // 封装成一个函数  需要知道让哪个元素动起来 
        // 往哪个方向移动  
        // 移动多久停下来 
        function move(ele,type,target){
            var init = 0;
            const timer = setInterval(()=>{
                init += 5;

                ele.style[type] = init + 'px';
                if(init>=target){
                    clearInterval(timer);
                }
            },50)
        }


        divele.onclick = function(){
            move(divele,'left',500);
        }

        document.querySelector('p').onclick = function(){
            move(this,'top',500);
        }
    </script>
</body>
</html>